//
//  TaskDataCenter.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/8.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "TaskDataCenter.h"

@implementation TaskDataCenter

- (void)pushTaskWithTaskModel:(TaskModel *)model friendModel:(FriendModel *)friendModel dateModel:(TaskDateModel *)dateModel complation:(void (^)(id))complation
{
    UserModel *user =[UserManager fetchUserInfo];
    NSDictionary *param = @{
                            @"name":model.name,
                            @"days":[NSNumber numberWithInteger:dateModel.taskDays],
                            @"user":[NSNumber numberWithInteger:user.userId],
                            @"friend":[NSNumber numberWithInteger:friendModel.friendId],
                            @"image":model.image,
                            @"description":model.descriptionText,
                            @"sendword":model.sendwordText,
                            @"startdate":dateModel.startDate,
                            @"enddate":dateModel.endDate,
                            };
    
    [HttpRequest POSTRequest:PushTask WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

- (void)fetchTaskListWithPage:(NSInteger)page Complation:(void (^)(id, NSString *))complation
{
    UserModel *user =[UserManager fetchUserInfo];
    NSDictionary *param = @{
                            @"user":[NSNumber numberWithInteger:user.userId],
                            @"page":[NSNumber numberWithInteger:page]
                            };
    [HttpRequest POSTRequest:TaskList WithParams:param WithSuccess:^(id responseOjbect) {
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            NSArray *dataArray = (NSArray *)[responseOjbect objectForKey:@"data"];
            NSArray *modelArray = [NSArray yy_modelArrayWithClass:[TaskModel class] json:dataArray];
            complation(modelArray,nil);
            
        }else{
            complation(nil,[responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(nil,@"服务器异常");
    }];
}

- (void)receiveTask:(TaskModel *)task complation:(void (^)(NSString *))complation
{
    NSDictionary *param = @{
                            @"tasknumber":task.number,
                            };
    [HttpRequest POSTRequest:ReceiveTask WithParams:param WithSuccess:^(id responseOjbect) {
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

- (void)fetchMineTaksListWithPage:(NSInteger)page complation:(void (^)(id, NSString *))complation
{
    UserModel *user =[UserManager fetchUserInfo];
    NSDictionary *param = @{
                            @"user":[NSNumber numberWithInteger:user.userId],
                            @"page":[NSNumber numberWithInteger:page]
                            };
    [HttpRequest POSTRequest:MineTaskList WithParams:param WithSuccess:^(id responseOjbect) {
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            NSArray *dataArray = (NSArray *)[responseOjbect objectForKey:@"data"];
            NSArray *modelArray = [NSArray yy_modelArrayWithClass:[TaskModel class] json:dataArray];
            complation(modelArray,nil);
            
        }else{
            complation(nil,[responseOjbect objectForKey:@"message"]);
        }
    } WithFail:^(NSError *error) {
        complation(nil,@"服务器异常");
    }];
}

- (void)fetchTaskDetail:(TaskModel *)task complation:(void (^)(id, NSString *))complation
{
    NSDictionary *param = @{
                            @"tasknumber":task.number,
                            @"user":[NSNumber numberWithInteger:task.friendId],
                            };
    [HttpRequest POSTRequest:SignList WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            NSArray *dataArray = (NSArray *)[responseOjbect objectForKey:@"data"];
            NSArray *modelArray = [NSArray yy_modelArrayWithClass:[SignModel class] json:dataArray];
            complation(modelArray, nil);
        }else{
            complation(nil,[responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(nil,@"服务器异常");
    }];
}

@end
